/*******************************************************************************
 * Copyright (c) 2009 Dop Sun.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Dop Sun - initial API and implementation
 ******************************************************************************/
package com.softao.ming.gde.internal.core.natures;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectNature;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;

/**
 * Adds this nature into the project to enable GDE support to the project.
 */
public class GdeProject implements IProjectNature {
	/** ID of this nature */
	public static final String ID = "com.softao.ming.gde.GdeNature";
	
	private IProject mProject;
	
	@Override
	public void configure() throws CoreException {
		IFile file = mProject.getFile(GdePluginFile.FILE_NAME);
		if (!file.exists()) {
			GdePluginFile pluginFile = new GdePluginFile();
			String xmlString = pluginFile.toXmlString();
			try {
				byte[] bytes = xmlString.getBytes("UTF-8");
				ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
				file.create(inputStream, true, new NullProgressMonitor());
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void deconfigure() throws CoreException {

	}

	@Override
	public IProject getProject() {
		return mProject;
	}

	@Override
	public void setProject(IProject project) {
		mProject = project;
	}
}
